﻿(function () {
    var index = 0;
    $.extend({
        CommonLib: {
            //全局对象仅仅存在register函数，参数为名称空间全路径。
            NameSpaceRegister: function (nameSpaceName) {
                //使用.切割命名空间名称
                var nsArray = nameSpaceName.split('.');
                var sEval = "";
                var sNS = "";
                //循环命名空间的各个部分依次构建命名空间的各个部分
                for (var i = 0; i < nsArray.length; i++) {
                    if (i != 0) {
                        sNS += ".";
                    }
                    sNS += nsArray[i];
                    sEval += "if (typeof(" + sNS + ")=='undefined')"
                                + "{ " + sNS + "=new Object(); }";
                    continue;
                }
                if (sEval.length > 0)
                    eval(sEval);
            },
            //对象转化为str
            ObjToString: function (o) {
                var r = [];
                var otype = "undefined";
                if (typeof o == "string") {
                    return "\"" + o.replace(/(['\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
                }
                if (typeof o == "undefined") {
                    return otype;
                }
                if (typeof o == "object") {
                    if (o === null) return "null";
                    else if (!o.sort) {
                        for (var i in o) {
                            r.push('\"' + i + '\":' + this.ObjToString(o[i]));
                        }
                        r = "{" + r.join() + "}";
                    } else {
                        for (var i = 0; i < o.length; i++) {
                            r.push(this.ObjToString(o[i]));
                        }
                        r = "[" + r.join() + "]";
                    }
                    return r;
                }
                return o.toString();
            },
            //replaceAll方法
            ReplaceAll: function (s, s1, s2) {
                return s.replace(new RegExp(s1, "gm"), s2);
            },
            //时间转换
            ChangeDateFormat: function (cellval) {
                var date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));
                var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
                var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
                var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
                var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
                var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
                return date.getFullYear() + "-" + month + "-" + currentDate + " " + hour + ":" + minutes + ":" + second;
            },
            //ajax提交
            Ajax: function (option) {
                var param = {
                    IsAsync: "true",
                    url: "",
                    params: [],
                    callback: null
                };
                $.extend(param, option);
                if (param.params != null && param.params != "") {
                    $.ajax({
                        type: "POST",
                        url: param.url,
                        dataType: "json",
                        data: $.CommonLib.ObjToString(param.params),
                        async: param.IsAsync,
                        processData: false,
                        success: function (html) {
                            if (typeof param.callback == "function")
                                param.callback(html);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert("找不到服务器!");
                        }
                    });
                }
                else {
                    $.ajax({
                        type: "GET",
                        url: param.url,
                        dataType: "json",
                        async: param.IsAsync,
                        success: function (html) {
                            if (typeof param.callback == "function")
                                param.callback(html);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            //alert("找不到服务器!");
                        }
                    });
                }
            },
            //将格式为[{"year":"2011"},{"month":"12"}]的参数转化为系统设定的键值对的格式
            FormatParam: function (arr) {
                if (!arr || arr.length == 0) { arr = new Array(); }
                else {
                    var paraArray = new Array();
                    for (var i = 0; i < arr.length; i++) {
                        for (var key in arr[i]) {
                            paraArray.push({ "Key": key, "Value": arr[i][key] });
                        }
                    }
                }
                return paraArray;
            },
            ParseJSON: function (data) {
                if (typeof data !== "string" || !data) {
                    return null;
                }
                data = data.replace(/^\s+|\s+$/, '');
                if (window.JSON && window.JSON.parse) {
                    return window.JSON.parse(data);
                }
                var rvalidchars = /^[\],:{}\s]*$/,
                  rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
                  rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
                   rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g;
                if (rvalidchars.test(data.replace(rvalidescape, "@").replace(rvalidtokens, "]").replace(rvalidbraces, ""))) {
                    return (new Function("return " + data))();
                }
                return null;
            },
            //获取窗体宽度
            GetTotalWidth: function () {
                return $(window).width();
            }
        }
    })
})(jQuery);
var commonLib = $.CommonLib;